﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊处方类别
    /// </summary>
    public class OtpRxTypeService : CrudAppService<OtpRxType, OtpRxTypeDto, Guid, OtpRxTypeInput, OtpRxTypeCreateOrUpdateDto, OtpRxTypeCreateOrUpdateDto>, IOtpRxTypeService
    {
        private IRepository<OtpRxType, Guid> _optSchdTypeRepository;
        private IOtpRgstRecService _otpRgstRecService;

        public OtpRxTypeService(IRepository<OtpRxType, Guid> optSchdTypeRepository
            , IOtpRgstRecService otpRgstRecService) : base(optSchdTypeRepository)
        {
            _optSchdTypeRepository = optSchdTypeRepository;
            _otpRgstRecService = otpRgstRecService;
        }

        public override async Task<PagedResultDto<OtpRxTypeDto>> GetListAsync(OtpRxTypeInput input)
        {
            var iQuery = await _optSchdTypeRepository.GetQueryableAsync();
            //iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), d => d.OtpRxTypeCodg.Contains(input.Filter) || d.OtpRxTypeName.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpRxType>, List<OtpRxTypeDto>>(iQuery.ToList());

                return new PagedResultDto<OtpRxTypeDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpRxType>, List<OtpRxTypeDto>>(items.ToList());

                return new PagedResultDto<OtpRxTypeDto>(totalCount, itemsDto);
            }
        }
    }
}
